package org.ethan.user.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.ethan.basic.util.AjaxResult;
import org.ethan.basic.util.PageList;
import org.ethan.user.domain.LoginInfo;
import org.ethan.user.query.LoginInfoQuery;
import org.ethan.user.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 登录操作
 */
@RestController // @Controller @ResponseBody
@RequestMapping("/logininfo")
@Api(tags = "登录接口",description = "登录接口详细描述")
public class LoginInfoController {

    @Autowired
    private ILoginInfoService logininfoService;

    /**
     *  url: localhost:8080/logininfo/query?id=1  以前的方式
     *  url: localhost:8080/logininfo/1  restful的方式
     * @param id
     * @return
     */
    // @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据",notes = "传一个id")
    public LoginInfo queryById(@PathVariable("id") Long id){
        return logininfoService.getById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有",notes = "不需要参数")
    public List<LoginInfo> queryAll(){
        return logininfoService.getAll();
    }

    /**
     * @param query
     * @return
     *  {
     *      currentPage: 1,
     *      pageSize: 10,
     *      ....
     *  }
     *  查询分页数据
     */
    @PostMapping
    @ApiOperation(value = "分页查询",notes = "分页。。。")
    public PageList<LoginInfo> queryPage(@RequestBody LoginInfoQuery query){
        return logininfoService.queryData(query);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "传一个id")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            logininfoService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除",notes = "传id数组")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            logininfoService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙。。");
        }
    }

    /**
     * 添加和修改操作
     * @param logininfo
     * @return
     */
    @PutMapping
    @ApiOperation(value = "登录添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addAndUpdate(@RequestBody LoginInfo logininfo){
        try {
            if(logininfo.getId() == null){ // 添加操作
                logininfoService.add(logininfo);
            }else{ // 修改操作
                logininfoService.update(logininfo);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙");
        }
    }


}

